package com.tms.app.tms.db;


import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.bean.Msg;
import com.tms.app.tms.bean.TaskInfo;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2017/12/8 0008.
 */

public class TaskInfoDBUtil extends DBUtil {


    public static void deleleTask(int planStatus){
        deleleTask(planStatus,0);
    }
    public static void deleleTask(int planStatus,int allotPlanId){
        String mobile= SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"mobile","");
        if (mobile==null||mobile.equals(""))
            return ;
        WhereBuilder whereBuilder=WhereBuilder.b("planStatus","=",planStatus)
                .and("mobile","=",mobile);
        if (allotPlanId>0){
            whereBuilder. and("allotPlanId","=",allotPlanId);
        }
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            db.delete(TaskInfo.class, whereBuilder);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }




    /****
     * 切换执行中任务为已完成
     * @param allotPlanId
     */
    public static void upadateTaskStatusIf(int allotPlanId){
        //派车单状态 0 待执行，1执行中，2 司机已完成3，确认已完成4 挂起
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            TaskInfo loalTask=db.selector(TaskInfo.class).where("allotPlanId","=",allotPlanId)
            //  .and("allotPlanId","=",taskInfo.getAllotPlanId())
            // .and("allotPlanNo","=",taskInfo.getAllotPlanNo())
              .and("planStatus","=",1)
            .findFirst();
            if (loalTask==null||loalTask.getId()<1)
            {
                return;
            }
            loalTask.setPlanStatus(2);
            db.saveOrUpdate(loalTask);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static void updateTaskReadType(int allotPlanId,int readType){
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            List<TaskInfo> list=db.selector(TaskInfo.class).where("allotPlanId","=",allotPlanId).findAll();
                    //  .and("allotPlanId","=",taskInfo.getAllotPlanId())
                    // .and("allotPlanNo","=",taskInfo.getAllotPlanNo())
                  //  .and("mobile","=",mobile).findFirst();
            if (list != null&&list.size()>0) {
                for (TaskInfo taskInfo : list) {
                    taskInfo.setIsRead(readType);
                    db.saveOrUpdate(taskInfo);
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static void saveOrUpadateTask(int allotPlanId,int planStatus){
        String mobile= SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"mobile","");
        if (mobile==null||mobile.equals(""))
            return ;
        DbManager db = x.getDb(MyApp.getDaoConfig());
        TaskInfo loalTask= null;
        try {
            loalTask = db.selector(TaskInfo.class).where("allotPlanId","=",allotPlanId).findFirst();
        //  .and("allotPlanId","=",taskInfo.getAllotPlanId())
        // .and("allotPlanNo","=",taskInfo.getAllotPlanNo())
        //  .and("mobile","=",mobile).findFirst();
        if (loalTask==null||loalTask.getId()<1)
        {
            return;
        }
        loalTask.setPlanStatus(planStatus);
        db.saveOrUpdate(loalTask);
        } catch (DbException e) {
            e.printStackTrace();
        }


    }
    public static void saveOrUpadateTask(TaskInfo taskInfo,int planStatus){
        String mobile= SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"mobile","");
        if (mobile==null||mobile.equals(""))
            return ;
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            TaskInfo loalTask=db.selector(TaskInfo.class).where("allotPlanId","=",taskInfo.getAllotPlanId())
                  //  .and("allotPlanId","=",taskInfo.getAllotPlanId())
                   // .and("allotPlanNo","=",taskInfo.getAllotPlanNo())
                    .and("mobile","=",mobile).findFirst();
             if (loalTask!=null&&loalTask.getId()>0)
                {
                    taskInfo.setId(loalTask.getId());
            }
            taskInfo.setMobile(mobile);
            taskInfo.setPlanStatus(planStatus);
            db.saveOrUpdate(taskInfo);
        } catch (DbException e) {
            e.printStackTrace();
        }


    }
    public static void saveOrUpadateTask(List<TaskInfo> list,int planStatus){
        saveOrUpadateTask(list,planStatus,true);
    }
    public static void saveOrUpadateTask(List<TaskInfo> list,int planStatus,boolean beforeDelele){
        if (beforeDelele){
            deleleTask(planStatus);
        }
        for (TaskInfo msg : list) {
            saveOrUpadateTask(msg,planStatus);
        }
    }

    /***
     * 获取待执行列表
     * @return
     */
    public static  List<TaskInfo>  getTaskList(){
        return getTaskList(0);
    }
    public static  List<TaskInfo>  getTaskList(int  planStatus){

        String mobile= SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"mobile","");
        if (mobile==null||mobile.equals(""))
            return null;
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            List<TaskInfo> listSort = db.selector(TaskInfo.class).where("planStatus","=",planStatus)
                    .and("mobile","=",mobile)
                    .orderBy("publishTime",true).findAll();
            return listSort;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static  List<TaskInfo>  getHistoryTaskList(){
        String mobile= SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"mobile","");
        if (mobile==null||mobile.equals(""))
            return null;
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            List<TaskInfo> listSort = db.selector(TaskInfo.class).where("planStatus","=",2)
//                    .or("planStatus","=",3)
                    .and("mobile","=",mobile).orderBy("publishTime",true).findAll();
            return listSort;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }



    public static boolean checkTaskdoing(){
        //派车单状态 0 待执行，1执行中，2 司机已完成3，确认已完成4 挂起
        String mobile= SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"mobile","");
        if (mobile==null||mobile.equals(""))
            return false;
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            TaskInfo loalTask=db.selector(TaskInfo.class).where("mobile","=",mobile)
                    //  .and("allotPlanId","=",taskInfo.getAllotPlanId())
                    // .and("allotPlanNo","=",taskInfo.getAllotPlanNo())
                    .and("planStatus","=",1)
                    .findFirst();
            if (loalTask==null||loalTask.getId()<1)
            {
                return false;
            }else {
                return true;
            }
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    /******
     * 获取执行中任务
     * @return
     */
    public static  TaskInfo  getTaskInfo() {
        return getTaskInfo(1);
    }
    public static  TaskInfo  getTaskInfo(int  planStatus){
        String mobile= SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"mobile","");
        if (mobile==null||mobile.equals(""))
            return null;
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            TaskInfo listSort = db.selector(TaskInfo.class)
                    .where("planStatus","=",planStatus)
                    .and("mobile","=",mobile)
                    .findFirst();
            return listSort;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }
}
